package com.wswy.wyjk.ui.practiceNew;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.my.httpapi.api.annotation.ViewL;
import com.my.httpapi.api.baseView.BaseActivity;
import com.tencent.mmkv.MMKV;
import com.wswy.wyjk.R;
import com.wswy.wyjk.api.MMKVConstant;
import com.wswy.wyjk.ui.practiceNew.Model.ExaminationRecord;
import com.wswy.wyjk.ui.practiceNew.Model.FractionRecord;
import com.wswy.wyjk.utils.Tools;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@ViewL(R.layout.fragment_exam_result)
public class ExamResultActivity extends BaseActivity {
    private TextView tvExamTime, tvExamScore, tvExamStatus, tvCtInfo;
    private View layoutSeeWrongQuestions, layoutReExam;
    private int rightCount = 0, wrongCount = 0;
    private long time;
    private int count;
    private ExaminationRecord examinationRecord;

    @Override
    protected void initData(Bundle savedInstanceState) {
        initView();
        initData();
        initListener();
    }

    @Override
    protected String showTitle() {
        return "考试结果";
    }

    @Override
    protected boolean statusBarIsBlack() {
        return true;
    }

    @Override
    protected void destroy() {
        ExamActivity.practiceErrList.clear();
    }

    private void initView() {
        isShowTitleLeft(View.VISIBLE);
        tvExamTime = findViewById(R.id.tv_exam_time);
        tvExamScore = findViewById(R.id.tv_exam_score);
        tvExamStatus = findViewById(R.id.tv_exam_status);
        tvCtInfo = findViewById(R.id.tv_ct_info);
        layoutSeeWrongQuestions = findViewById(R.id.layout_see_wrong_questions);
        layoutReExam = findViewById(R.id.layout_re_exam);
    }

    private void initData() {
        rightCount = getIntent().getIntExtra("right", -1);
        wrongCount = getIntent().getIntExtra("wrong", -1);
        time = getIntent().getLongExtra("time", -1);
        count = getIntent().getIntExtra("count", -1);

        tvExamTime.setText("用时：" + Tools.formatSecondTime(time / 1000));
        if (count == 100) {
            tvExamStatus.setText(rightCount >= 90 ? "合格" : "不合格");
            tvExamScore.setText(rightCount + "");
        } else {
            tvExamStatus.setText((rightCount * 2) >= 90 ? "合格" : "不合格");
            tvExamScore.setText((rightCount * 2) + "");
        }
        tvCtInfo.setText("答错" + wrongCount + "题，未做" + (count - wrongCount - rightCount) + "题");
        //记录相关的
        String record = MMKV.defaultMMKV().decodeString(MMKVConstant.RECORD);
        ExaminationRecord examinationRecord = new Gson().fromJson(record, ExaminationRecord.class);
        if (examinationRecord == null) {
            examinationRecord = new ExaminationRecord();
        }
        List<FractionRecord> list = examinationRecord.getList();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = new Date(System.currentTimeMillis());
        FractionRecord fractionRecord = new FractionRecord(Integer.valueOf(tvExamScore.getText().toString()), Tools.formatSecondTime(time / 1000), simpleDateFormat.format(date));
        list.add(fractionRecord);
        examinationRecord.setList(list);
        int max = examinationRecord.getMaxFraction();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getFraction() > max) {
                max = list.get(i).getFraction();
            }
        }
        examinationRecord.setMaxFraction(max);
        MMKV.defaultMMKV().encode(MMKVConstant.RECORD, new Gson().toJson(examinationRecord));
    }

    private void initListener() {
        layoutSeeWrongQuestions.setOnClickListener(view -> {
            if (ExamActivity.practiceErrList.size() == 0) return;
            Map<String, Object> map = new HashMap<>();
            map.put("type", 5);
            intent(PracticeNewActivity.class, map);
        });

        layoutReExam.setOnClickListener(view -> {
            intentFinish(ExamActivity.class);
        });
    }
}
